<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveSupport::SecureRandom</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveSupport::SecureRandom</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/activesupport/lib/active_support/secure_random_rb.html">
                vendor/rails/activesupport/lib/active_support/secure_random.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <h1>Secure random number generator interface.</h1>
<p>
This library is an interface for secure random number generator which is
suitable for generating session key in HTTP cookies, etc.
</p>
<p>
It supports following secure random number generators.
</p>
<ul>
<li>openssl

</li>
<li>/dev/urandom

</li>
<li>Win32

</li>
</ul>
<p>
<b>Note</b>: This module is based on the <a
href="SecureRandom.html">SecureRandom</a> library from Ruby 1.9, revision
18786, August 23 2008. It&#8216;s 100% interface-compatible with Ruby
1.9&#8216;s <a href="SecureRandom.html">SecureRandom</a> library.
</p>
<h2>Example</h2>
<pre>
 # random hexadecimal string.
 p SecureRandom.hex(10) #=&gt; &quot;52750b30ffbc7de3b362&quot;
 p SecureRandom.hex(10) #=&gt; &quot;92b15d6c8dc4beb5f559&quot;
 p SecureRandom.hex(11) #=&gt; &quot;6aca1b5c58e4863e6b81b8&quot;
 p SecureRandom.hex(12) #=&gt; &quot;94b2fff3e7fd9b9c391a2306&quot;
 p SecureRandom.hex(13) #=&gt; &quot;39b290146bea6ce975c37cfc23&quot;
 ...

 # random base64 string.
 p SecureRandom.base64(10) #=&gt; &quot;EcmTPZwWRAozdA==&quot;
 p SecureRandom.base64(10) #=&gt; &quot;9b0nsevdwNuM/w==&quot;
 p SecureRandom.base64(10) #=&gt; &quot;KO1nIU+p9DKxGg==&quot;
 p SecureRandom.base64(11) #=&gt; &quot;l7XEiFja+8EKEtY=&quot;
 p SecureRandom.base64(12) #=&gt; &quot;7kJSM/MzBJI+75j8&quot;
 p SecureRandom.base64(13) #=&gt; &quot;vKLJ0tXBHqQOuIcSIg==&quot;
 ...

 # random binary string.
 p SecureRandom.random_bytes(10) #=&gt; &quot;\016\t{\370g\310pbr\301&quot;
 p SecureRandom.random_bytes(10) #=&gt; &quot;\323U\030TO\234\357\020\a\337&quot;
 ...
</pre>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000402">base64</a>&nbsp;&nbsp;
      <a href="#M000401">hex</a>&nbsp;&nbsp;
      <a href="#M000400">random_bytes</a>&nbsp;&nbsp;
      <a href="#M000403">random_number</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M000402" class="method-detail">
        <a name="M000402"></a>

        <div class="method-heading">
          <a href="#M000402" class="method-signature">
          <span class="method-name">base64</span><span class="method-args">(n=nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
<a href="SecureRandom.html#M000402">SecureRandom.base64</a> generates a
random <a href="SecureRandom.html#M000402">base64</a> string.
</p>
<p>
The argument n specifies the length of the random length. The length of the
result string is about 4/3 of n.
</p>
<p>
If n is not specified, 16 is assumed. It may be larger in future.
</p>
<p>
If secure random number generator is not available, NotImplementedError is
raised.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000402-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000402-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/secure_random.rb, line 149</span>
149:       <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">base64</span>(<span class="ruby-identifier">n</span>=<span class="ruby-keyword kw">nil</span>)
150:         [<span class="ruby-identifier">random_bytes</span>(<span class="ruby-identifier">n</span>)].<span class="ruby-identifier">pack</span>(<span class="ruby-value str">&quot;m*&quot;</span>).<span class="ruby-identifier">delete</span>(<span class="ruby-value str">&quot;\n&quot;</span>)
151:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000401" class="method-detail">
        <a name="M000401"></a>

        <div class="method-heading">
          <a href="#M000401" class="method-signature">
          <span class="method-name">hex</span><span class="method-args">(n=nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
<a href="SecureRandom.html#M000401">SecureRandom.hex</a> generates a random
<a href="SecureRandom.html#M000401">hex</a> string.
</p>
<p>
The argument n specifies the length of the random length. The length of the
result string is twice of n.
</p>
<p>
If n is not specified, 16 is assumed. It may be larger in future.
</p>
<p>
If secure random number generator is not available, NotImplementedError is
raised.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000401-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000401-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/secure_random.rb, line 135</span>
135:       <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">hex</span>(<span class="ruby-identifier">n</span>=<span class="ruby-keyword kw">nil</span>)
136:         <span class="ruby-identifier">random_bytes</span>(<span class="ruby-identifier">n</span>).<span class="ruby-identifier">unpack</span>(<span class="ruby-value str">&quot;H*&quot;</span>)[<span class="ruby-value">0</span>]
137:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000400" class="method-detail">
        <a name="M000400"></a>

        <div class="method-heading">
          <a href="#M000400" class="method-signature">
          <span class="method-name">random_bytes</span><span class="method-args">(n=nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
<a href="SecureRandom.html#M000400">SecureRandom.random_bytes</a> generates
a random binary string.
</p>
<p>
The argument n specifies the length of the result string.
</p>
<p>
If n is not specified, 16 is assumed. It may be larger in future.
</p>
<p>
If secure random number generator is not available, NotImplementedError is
raised.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000400-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000400-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/secure_random.rb, line 64</span>
 64:       <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">random_bytes</span>(<span class="ruby-identifier">n</span>=<span class="ruby-keyword kw">nil</span>)
 65:         <span class="ruby-identifier">n</span> <span class="ruby-operator">||=</span> <span class="ruby-value">16</span>
 66: 
 67:         <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">defined?</span> <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">Random</span>
 68:           <span class="ruby-keyword kw">return</span> <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">Random</span>.<span class="ruby-identifier">random_bytes</span>(<span class="ruby-identifier">n</span>)
 69:         <span class="ruby-keyword kw">end</span>
 70: 
 71:         <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-keyword kw">defined?</span>(<span class="ruby-ivar">@has_urandom</span>) <span class="ruby-operator">||</span> <span class="ruby-ivar">@has_urandom</span>
 72:           <span class="ruby-identifier">flags</span> = <span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">RDONLY</span>
 73:           <span class="ruby-identifier">flags</span> <span class="ruby-operator">|=</span> <span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">NONBLOCK</span> <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">defined?</span> <span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">NONBLOCK</span>
 74:           <span class="ruby-identifier">flags</span> <span class="ruby-operator">|=</span> <span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">NOCTTY</span> <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">defined?</span> <span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">NOCTTY</span>
 75:           <span class="ruby-identifier">flags</span> <span class="ruby-operator">|=</span> <span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">NOFOLLOW</span> <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">defined?</span> <span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">NOFOLLOW</span>
 76:           <span class="ruby-keyword kw">begin</span>
 77:             <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-value str">&quot;/dev/urandom&quot;</span>, <span class="ruby-identifier">flags</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
 78:               <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">f</span>.<span class="ruby-identifier">stat</span>.<span class="ruby-identifier">chardev?</span>
 79:                 <span class="ruby-identifier">raise</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">ENOENT</span>
 80:               <span class="ruby-keyword kw">end</span>
 81:               <span class="ruby-ivar">@has_urandom</span> = <span class="ruby-keyword kw">true</span>
 82:               <span class="ruby-identifier">ret</span> = <span class="ruby-identifier">f</span>.<span class="ruby-identifier">readpartial</span>(<span class="ruby-identifier">n</span>)
 83:               <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">ret</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">n</span>
 84:                 <span class="ruby-identifier">raise</span> <span class="ruby-constant">NotImplementedError</span>, <span class="ruby-value str">&quot;Unexpected partial read from random device&quot;</span>
 85:               <span class="ruby-keyword kw">end</span>
 86:               <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">ret</span>
 87:             }
 88:           <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">ENOENT</span>
 89:             <span class="ruby-ivar">@has_urandom</span> = <span class="ruby-keyword kw">false</span>
 90:           <span class="ruby-keyword kw">end</span>
 91:         <span class="ruby-keyword kw">end</span>
 92: 
 93:         <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-keyword kw">defined?</span>(<span class="ruby-ivar">@has_win32</span>)
 94:           <span class="ruby-keyword kw">begin</span>
 95:             <span class="ruby-identifier">require</span> <span class="ruby-value str">'Win32API'</span>
 96: 
 97:             <span class="ruby-identifier">crypt_acquire_context</span> = <span class="ruby-constant">Win32API</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value str">&quot;advapi32&quot;</span>, <span class="ruby-value str">&quot;CryptAcquireContext&quot;</span>, <span class="ruby-value str">'PPPII'</span>, <span class="ruby-value str">'L'</span>)
 98:             <span class="ruby-ivar">@crypt_gen_random</span> = <span class="ruby-constant">Win32API</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value str">&quot;advapi32&quot;</span>, <span class="ruby-value str">&quot;CryptGenRandom&quot;</span>, <span class="ruby-value str">'LIP'</span>, <span class="ruby-value str">'L'</span>)
 99: 
100:             <span class="ruby-identifier">hProvStr</span> = <span class="ruby-value str">&quot; &quot;</span> <span class="ruby-operator">*</span> <span class="ruby-value">4</span>
101:             <span class="ruby-identifier">prov_rsa_full</span> = <span class="ruby-value">1</span>
102:             <span class="ruby-identifier">crypt_verifycontext</span> = <span class="ruby-value">0x</span><span class="ruby-constant">F0000000</span>
103: 
104:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">crypt_acquire_context</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">hProvStr</span>, <span class="ruby-keyword kw">nil</span>, <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">prov_rsa_full</span>, <span class="ruby-identifier">crypt_verifycontext</span>) <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
105:               <span class="ruby-identifier">raise</span> <span class="ruby-constant">SystemCallError</span>, <span class="ruby-node">&quot;CryptAcquireContext failed: #{lastWin32ErrorMessage}&quot;</span>
106:             <span class="ruby-keyword kw">end</span>
107:             <span class="ruby-ivar">@hProv</span>, = <span class="ruby-identifier">hProvStr</span>.<span class="ruby-identifier">unpack</span>(<span class="ruby-value str">'L'</span>)
108: 
109:             <span class="ruby-ivar">@has_win32</span> = <span class="ruby-keyword kw">true</span>
110:           <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">LoadError</span>
111:             <span class="ruby-ivar">@has_win32</span> = <span class="ruby-keyword kw">false</span>
112:           <span class="ruby-keyword kw">end</span>
113:         <span class="ruby-keyword kw">end</span>
114:         <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@has_win32</span>
115:           <span class="ruby-identifier">bytes</span> = <span class="ruby-value str">&quot; &quot;</span> <span class="ruby-operator">*</span> <span class="ruby-identifier">n</span>
116:           <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@crypt_gen_random</span>.<span class="ruby-identifier">call</span>(<span class="ruby-ivar">@hProv</span>, <span class="ruby-identifier">bytes</span>.<span class="ruby-identifier">size</span>, <span class="ruby-identifier">bytes</span>) <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
117:             <span class="ruby-identifier">raise</span> <span class="ruby-constant">SystemCallError</span>, <span class="ruby-node">&quot;CryptGenRandom failed: #{lastWin32ErrorMessage}&quot;</span>
118:           <span class="ruby-keyword kw">end</span>
119:           <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">bytes</span>
120:         <span class="ruby-keyword kw">end</span>
121: 
122:         <span class="ruby-identifier">raise</span> <span class="ruby-constant">NotImplementedError</span>, <span class="ruby-value str">&quot;No random device&quot;</span>
123:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000403" class="method-detail">
        <a name="M000403"></a>

        <div class="method-heading">
          <a href="#M000403" class="method-signature">
          <span class="method-name">random_number</span><span class="method-args">(n=0)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
<a href="SecureRandom.html#M000403">SecureRandom.random_number</a>
generates a random number.
</p>
<p>
If an positive integer is given as n, <a
href="SecureRandom.html#M000403">SecureRandom.random_number</a> returns an
integer: 0 &lt;= <a
href="SecureRandom.html#M000403">SecureRandom.random_number(n)</a> &lt; n.
</p>
<p>
If 0 is given or an argument is not given, <a
href="SecureRandom.html#M000403">SecureRandom.random_number</a> returns an
float: 0.0 &lt;= <a
href="SecureRandom.html#M000403">SecureRandom.random_number</a>() &lt; 1.0.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000403-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000403-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/secure_random.rb, line 162</span>
162:       <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">random_number</span>(<span class="ruby-identifier">n</span>=<span class="ruby-value">0</span>)
163:         <span class="ruby-keyword kw">if</span> <span class="ruby-value">0</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">n</span>
164:           <span class="ruby-identifier">hex</span> = <span class="ruby-identifier">n</span>.<span class="ruby-identifier">to_s</span>(<span class="ruby-value">16</span>)
165:           <span class="ruby-identifier">hex</span> = <span class="ruby-value str">'0'</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">hex</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">hex</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&amp;</span> <span class="ruby-value">1</span>) <span class="ruby-operator">==</span> <span class="ruby-value">1</span>
166:           <span class="ruby-identifier">bin</span> = [<span class="ruby-identifier">hex</span>].<span class="ruby-identifier">pack</span>(<span class="ruby-value str">&quot;H*&quot;</span>)
167:           <span class="ruby-identifier">mask</span> = <span class="ruby-identifier">bin</span>[<span class="ruby-value">0</span>]
168:           <span class="ruby-identifier">mask</span> <span class="ruby-operator">|=</span> <span class="ruby-identifier">mask</span> <span class="ruby-operator">&gt;&gt;</span> <span class="ruby-value">1</span>
169:           <span class="ruby-identifier">mask</span> <span class="ruby-operator">|=</span> <span class="ruby-identifier">mask</span> <span class="ruby-operator">&gt;&gt;</span> <span class="ruby-value">2</span>
170:           <span class="ruby-identifier">mask</span> <span class="ruby-operator">|=</span> <span class="ruby-identifier">mask</span> <span class="ruby-operator">&gt;&gt;</span> <span class="ruby-value">4</span>
171:           <span class="ruby-keyword kw">begin</span>
172:             <span class="ruby-identifier">rnd</span> = <span class="ruby-constant">SecureRandom</span>.<span class="ruby-identifier">random_bytes</span>(<span class="ruby-identifier">bin</span>.<span class="ruby-identifier">length</span>)
173:             <span class="ruby-identifier">rnd</span>[<span class="ruby-value">0</span>] = <span class="ruby-identifier">rnd</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">&amp;</span> <span class="ruby-identifier">mask</span>
174:           <span class="ruby-keyword kw">end</span> <span class="ruby-keyword kw">until</span> <span class="ruby-identifier">rnd</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">bin</span>
175:           <span class="ruby-identifier">rnd</span>.<span class="ruby-identifier">unpack</span>(<span class="ruby-value str">&quot;H*&quot;</span>)[<span class="ruby-value">0</span>].<span class="ruby-identifier">hex</span>
176:         <span class="ruby-keyword kw">else</span>
177:           <span class="ruby-comment cmt"># assumption: Float::MANT_DIG &lt;= 64</span>
178:           <span class="ruby-identifier">i64</span> = <span class="ruby-constant">SecureRandom</span>.<span class="ruby-identifier">random_bytes</span>(<span class="ruby-value">8</span>).<span class="ruby-identifier">unpack</span>(<span class="ruby-value str">&quot;Q&quot;</span>)[<span class="ruby-value">0</span>]
179:           <span class="ruby-constant">Math</span>.<span class="ruby-identifier">ldexp</span>(<span class="ruby-identifier">i64</span> <span class="ruby-operator">&gt;&gt;</span> (<span class="ruby-value">64</span><span class="ruby-operator">-</span><span class="ruby-constant">Float</span><span class="ruby-operator">::</span><span class="ruby-constant">MANT_DIG</span>), <span class="ruby-operator">-</span><span class="ruby-constant">Float</span><span class="ruby-operator">::</span><span class="ruby-constant">MANT_DIG</span>)
180:         <span class="ruby-keyword kw">end</span>
181:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>